package com.yanqu.road.dao.impl.activity.musicroomrank;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.activity.musicroomrank.CrossMusicRoomRankRoundData;
import com.yanqu.road.entity.activity.musicroomrank.CrossMusicRoomRankShowData;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class CrossMusicRoomRankShowDaoImpl extends TempDataObjectDao<CrossMusicRoomRankShowData> {

    public static String getCrossMusicRoomRankShowDataSql = "select * from `t_c_music_room_rank_show` where `activityId`=?";
    public Map<Integer, List<CrossMusicRoomRankShowData>> getShowDataMap(int activityId) {
        ResultSet rs = null;
        CallableStatement call = null;
        Map<Integer, List<CrossMusicRoomRankShowData>> dataMap = new ConcurrentHashMap<>();
        try {
            getDb().setConnName(DbNameStrategy.getCrossDb());
            call = getDb().getConnection().prepareCall(getCrossMusicRoomRankShowDataSql);
            int i = 1;
            call.setInt(i++, activityId);
            rs = call.executeQuery();
            while (rs.next()) {
                CrossMusicRoomRankShowData data = parseData(rs);
                List<CrossMusicRoomRankShowData> dataList = dataMap.get(data.getRound());
                if (dataList == null) {
                    dataList = new ArrayList<>();
                    dataMap.put(data.getRound(), dataList);
                }
                dataList.add(data);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return dataMap;
    }

    public static String updateCrossMusicRoomRankShowDataSql = "update `t_c_music_room_rank_show` set attrList=?,cardAmount=?,showScore=?,joinTime=?,joinPos=?,rank=?,rankScore=?,costType=?,buffAddScore=? where activityId=? and round=? and userId=? and beautyId=? and skinId=?";
    @Override
    protected boolean updateData(CrossMusicRoomRankShowData crossMusicRoomRankShowData) {
        crossMusicRoomRankShowData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getCrossDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(crossMusicRoomRankShowData.getAttrList())));
        para.put(i++, new DbParameter(Types.INTEGER, crossMusicRoomRankShowData.getCardAmount()));
        para.put(i++, new DbParameter(Types.BIGINT, crossMusicRoomRankShowData.getShowScore()));
        para.put(i++, new DbParameter(Types.BIGINT, crossMusicRoomRankShowData.getJoinTime()));
        para.put(i++, new DbParameter(Types.INTEGER, crossMusicRoomRankShowData.getJoinPos()));
        para.put(i++, new DbParameter(Types.INTEGER, crossMusicRoomRankShowData.getRank()));
        para.put(i++, new DbParameter(Types.BIGINT, crossMusicRoomRankShowData.getRankScore()));
        para.put(i++, new DbParameter(Types.INTEGER, crossMusicRoomRankShowData.getCostType()));
        para.put(i++, new DbParameter(Types.BIGINT, crossMusicRoomRankShowData.getBuffAddScore()));

        para.put(i++, new DbParameter(Types.INTEGER, crossMusicRoomRankShowData.getActivityId()));
        para.put(i++, new DbParameter(Types.INTEGER, crossMusicRoomRankShowData.getRound()));
        para.put(i++, new DbParameter(Types.BIGINT, crossMusicRoomRankShowData.getUserId()));
        para.put(i++, new DbParameter(Types.INTEGER, crossMusicRoomRankShowData.getBeautyId()));
        para.put(i++, new DbParameter(Types.INTEGER, crossMusicRoomRankShowData.getSkinId()));
        if (getDb().executeNoneQuery(updateCrossMusicRoomRankShowDataSql, para) > 0) {
            return true;
        }
        return false;
    }

    public static String insertCrossMusicRoomRankShowDataSql = "insert into `t_c_music_room_rank_show`(activityId,round,userId,beautyId,skinId,attrList,cardAmount,showScore,joinTime,joinPos,rank,rankScore,costType,buffAddScore) values (?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
    @Override
    protected boolean addData(CrossMusicRoomRankShowData crossMusicRoomRankShowData) {
        crossMusicRoomRankShowData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getCrossDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, crossMusicRoomRankShowData.getActivityId()));
        para.put(i++, new DbParameter(Types.INTEGER, crossMusicRoomRankShowData.getRound()));
        para.put(i++, new DbParameter(Types.BIGINT, crossMusicRoomRankShowData.getUserId()));
        para.put(i++, new DbParameter(Types.INTEGER, crossMusicRoomRankShowData.getBeautyId()));
        para.put(i++, new DbParameter(Types.INTEGER, crossMusicRoomRankShowData.getSkinId()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(crossMusicRoomRankShowData.getAttrList())));
        para.put(i++, new DbParameter(Types.INTEGER, crossMusicRoomRankShowData.getCardAmount()));
        para.put(i++, new DbParameter(Types.BIGINT, crossMusicRoomRankShowData.getShowScore()));
        para.put(i++, new DbParameter(Types.BIGINT, crossMusicRoomRankShowData.getJoinTime()));
        para.put(i++, new DbParameter(Types.INTEGER, crossMusicRoomRankShowData.getJoinPos()));
        para.put(i++, new DbParameter(Types.INTEGER, crossMusicRoomRankShowData.getRank()));
        para.put(i++, new DbParameter(Types.BIGINT, crossMusicRoomRankShowData.getRankScore()));
        para.put(i++, new DbParameter(Types.INTEGER, crossMusicRoomRankShowData.getCostType()));
        para.put(i++, new DbParameter(Types.BIGINT, crossMusicRoomRankShowData.getBuffAddScore()));
        if (getDb().executeNoneQuery(insertCrossMusicRoomRankShowDataSql, para) > 0) {
            return true;
        }
        return false;

    }

    @Override
    protected CrossMusicRoomRankShowData parseData(ResultSet rs) throws SQLException {
        CrossMusicRoomRankShowData crossMusicRoomRankShowData = new CrossMusicRoomRankShowData();
        crossMusicRoomRankShowData.setActivityId(rs.getInt("activityId"));
        crossMusicRoomRankShowData.setRound(rs.getInt("round"));
        crossMusicRoomRankShowData.setUserId(rs.getLong("userId"));
        crossMusicRoomRankShowData.setBeautyId(rs.getInt("beautyId"));
        crossMusicRoomRankShowData.setSkinId(rs.getInt("skinId"));
        crossMusicRoomRankShowData.setAttrList(JSON.parseObject(rs.getString("attrList"), new TypeReference<java.util.List<java.lang.Long>>() {}));
        crossMusicRoomRankShowData.setCardAmount(rs.getInt("cardAmount"));
        crossMusicRoomRankShowData.setShowScore(rs.getLong("showScore"));
        crossMusicRoomRankShowData.setJoinTime(rs.getLong("joinTime"));
        crossMusicRoomRankShowData.setJoinPos(rs.getInt("joinPos"));
        crossMusicRoomRankShowData.setRank(rs.getInt("rank"));
        crossMusicRoomRankShowData.setRankScore(rs.getLong("rankScore"));
        crossMusicRoomRankShowData.setCostType(rs.getInt("costType"));
        crossMusicRoomRankShowData.setBuffAddScore(rs.getLong("buffAddScore"));

        return crossMusicRoomRankShowData;
    }

}
